/*进入页面先刷新可租借的页面，url需更*/
$.ajax({
	type:"get",
	url:"findAllPlaces",
	success:function(result){
		for(var i=0;i<result.resultParm.places.length;i++){
				var newLine = 
					"<tr>"+
						"<td>"+result.resultParm.places[i].placeId+"</td>"+
						"<td>"+result.resultParm.places[i].placeName+"</td>"+
						"<td>"+result.resultParm.places[i].placeMoney+"</td>"+
						"<td>"+"<a data-toggle="+"\"modal\""+" href="+"\'#place-rent\'"+">预约</a>"+"</td>"
					"</tr>";
				$("table").append(newLine);
		};
		$(".place-table table a").click(function(){
			$("#placeId").val($(this).parent().parent().children().eq(0).text());
			$("#placeName").val($(this).parent().parent().children().eq(1).text());
			$("#rentTime").val(now);
		});
	},
	error:function(){
		alert("刷新失败");
	}
});
/*点击查询按钮执行的ajax，url需更改*/
$("#search-btn").click(function(){
	$(".place-table table tr td").remove();
	$.ajax({
		type:"post",
		url:"findAllByName",
		data:{
			name:$("#search-place").val()
		},
		success:function(result){
//			alert(result.resultParm.places[0].placeName);
			if(result.resultParm.length==0){
				alert("无搜索记录");
			}else{
				for(var i=0;i<result.resultParm.places.length;i++){
				var newLine = 
					"<tr>"+
						"<td>"+result.resultParm.places[i].placeId+"</td>"+
						"<td>"+result.resultParm.places[i].placeName+"</td>"+
						"<td>"+result.resultParm.places[i].placeMoney+"</td>"+
						"<td>"+"<a data-toggle="+"\"modal\""+" href="+"\'#place-rent\'"+">预约</a>"+"</td>"
					"</tr>";
				$("table").append(newLine);
				};
				$(".place-table table a").click(function(){
					$("#placeId").val($(this).parent().parent().children().eq(0).text());
					$("#placeName").val($(this).parent().parent().children().eq(1).text());
					$("#rentTime").val(now);
				});
			}
			
		},
		error:function(){
			alert("查询失败");
		}
	});
});
/*租借场地表单验证*/
$("#place-rent-modal").bootstrapValidator({
	message:"这是无效的数据域",
	feedbackIcons: {/*input状态样式图片*/
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields:{
    	placeId:{
    		message:"无效",
    		validators:{
    			notEmpty:{
    				message:"编号不可为空"
    			},
    			stringLength: {
                min: 3,
                max: 3,
                message: '编号为3位数'
                },
    		}
    	},
    	placeName: {
            message:'无效',
            validators: {
                notEmpty: {
                    message: '场地类型不可为空'
                },
            }
        },
        rentTime: {
            message:'时间无效',
            validators: {
                notEmpty: {
                    message: '时间不可为空'
                },
            }
        },
        endTime: {
            message:'时间无效',
            validators: {
                notEmpty: {
                    message: '时间不可为空'
                },
            }
        },
        placeUserName: {
            message:'无效',
            validators: {
                notEmpty: {
                    message: '姓名不能为空'
                },
                stringLength: {
                min: 2,
                max: 30,
                message: '用户名长度必须在3到30之间'
                },
            }
        },
        contact: {
            message:'无效',
            validators: {
                notEmpty: {
                    message: '联系方式不能为空'
                },
                stringLength: {
                    min: 11,
                    max: 11,
                    message: '请输入正确的手机号码'
                },
            }
        },
        placeUserId: {
            message:'无效',
            validators: {
                notEmpty: {
                    message: 'ID不能为空'
                },
                stringLength: {
                    min: 8,
                    max: 12,
                    message: '请输入正确的ID'
                },
            }
        },
    }
});

/*获得当前时间*/
function p(s) {
    return s < 10 ? '0' + s: s;
};
var myDate = new Date();
//获取当前年
var year=myDate.getFullYear();
//获取当前月
var month=myDate.getMonth()+1;
//获取当前日
var date=myDate.getDate(); 
var h=myDate.getHours();       //获取当前小时数(0-23)
var m=myDate.getMinutes();     //获取当前分钟数(0-59)
var s=myDate.getSeconds();  

var now=year+'-'+p(month)+"-"+p(date)+" "+p(h)+':'+p(m)+":"+p(s);
/*点击租借按钮*/
$("#place-btn").click(function(){
	$.ajax({
		type:"post",
		url:"addPlace",
		data:{
			startDate:$("#rentTime").val(),
			endDate:$("#endTime").val(),
			placeId:$("#placeId").val(),
			borrowAccount:$("#placeUserId").val(),
			borrowName:$("#placeUserName").val(),
			telephone:$("#contact").val()
		},
		success:function(result){
			alert(result.resultInfo);
			$("#place-rent").modal("hide");
		},
		eerror:function(result){
			alert(result.resultInfo);
		}
	});
});